package sps.admin.module.system.login.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.extra.servlet.ServletUtil;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import sps.admin.constant.AdminSwaggerTagConst;
import sps.admin.module.system.login.domain.LoginForm;
import sps.admin.module.system.login.domain.LoginResultVO;
import sps.admin.module.system.login.service.LoginService;
import sps.admin.util.AdminRequestUtil;
import sps.base.common.annoation.NoNeedLogin;
import sps.base.common.constant.RequestHeaderConst;
import sps.base.common.domain.ResponseDTO;
import sps.base.common.util.SmartRequestUtil;
import sps.base.module.support.captcha.domain.CaptchaVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.sql.*;

/**
 * 员工登录
 *
 * @Author 江苏圣普斯安防科技有限公司
 * @Date 2021-12-15 21:05:46
 * @Copyright  <a href="">江苏圣普斯安防科技有限公司</a>
 */
@RestController
@Tag(name = AdminSwaggerTagConst.System.SYSTEM_LOGIN)
public class LoginController {

    @Resource
    private LoginService loginService;

    @Value("${third.jiabao.api.driver}")
    String DBDRIVER;

    @Value("${third.jiabao.api.url}")
    String DBURL;

    @Value("${third.jiabao.api.username}")
    String USER;

    @Value("${third.jiabao.api.password}")
    String PASSWORD;

    @NoNeedLogin
    @PostMapping("/login")
    @Operation(summary = "登录 ")
    public ResponseDTO<LoginResultVO> login(@Valid @RequestBody LoginForm loginForm, HttpServletRequest request) {
        String ip = ServletUtil.getClientIP(request);
        String userAgent = ServletUtil.getHeaderIgnoreCase(request, RequestHeaderConst.USER_AGENT);
        return loginService.login(loginForm, ip, userAgent);
    }

    @GetMapping("/login/getLoginInfo")
    @Operation(summary = "获取登录结果信息  ")
    public ResponseDTO<LoginResultVO> getLoginInfo() {
        LoginResultVO loginResult = loginService.getLoginResult(AdminRequestUtil.getRequestUser());
        String tokenValue = StpUtil.getTokenValue();
        loginResult.setToken(tokenValue);
        return ResponseDTO.ok(loginResult);
    }

    @Operation(summary = "退出登陆  ")
    @GetMapping("/login/logout")
    public ResponseDTO<String> logout(@RequestHeader(value = RequestHeaderConst.TOKEN, required = false) String token) {
        return loginService.logout(token, SmartRequestUtil.getRequestUser());
    }

    @Operation(summary = "获取验证码  ")
    @GetMapping("/login/getCaptcha")
    @NoNeedLogin
    public ResponseDTO<CaptchaVO> getCaptcha() {
//        try {
//            //第一步：加载数据库驱动程序，此时不需要实例化，因为会由容器自己负责
//            Class.forName(DBDRIVER);
//            //第二步：根据连接协议、用户名、密码连接数据库
//            Connection conn = DriverManager.getConnection(DBURL,USER,PASSWORD);
//            System.out.println("连接成功!");
//            String sql = "SELECT * FROM attendancebook where the_date = ? ";
//            PreparedStatement preparedStatement = conn.prepareStatement(sql);
//            preparedStatement.setString(1,"2024.09.25");
//            ResultSet rs = preparedStatement.executeQuery( );//使用ResultSet接收数据
//
//            int index = 1;
//            while(rs.next()) {
//                String theDate = rs.getString("the_date");//加班日期
//                String empid = rs.getString("emp_id");//部门ID
//                String updatesql = "update attendancebook set  emp_name = '曹萍萍"+(index++)+"' where emp_id = '"+empid+"'";
//                Statement statement = conn.createStatement();
//                int i = statement.executeUpdate(updatesql);
//                System.out.println("更新成功，影响行数:" + i);//输出
//                System.out.println(theDate.trim() + " -->" + empid);//输出
//            }
//            conn.close();
//        } catch (ClassNotFoundException e) {
//            System.out.println("JDBC驱动未找到！");
//            e.printStackTrace();
//        } catch (SQLException e) {
//            System.out.println("数据库连接失败！");
//            e.printStackTrace();
//        }
        return loginService.getCaptcha();
    }

}
